package com.xbj.springmvc.core.util;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.xbj.springmvc.core.annotation.Controller;
import com.xbj.springmvc.core.annotation.RequestMapping;

public class LoadSpringMVCConfigUtil {

	public static Map<String, String> LoadSpringMVCConfig(String path) {
		SAXReader reader = new SAXReader();
		Map<String, String> map = new HashMap<String, String>();
		try {
			Document doc = reader.read(new File(path));
			Element root = doc.getRootElement();
			map.putAll(scanPackage(root));//扫描注解方式
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return map;
	}
	
	private static Map<String,String> scanPackage(Element root){
		//获取所有包
		String[] packages = getPackage(root);
		System.out.println("packages.length:"+packages.length);
		//获取包里所有类
		List<Class> classes = getClasses(packages);
		//解析类的注解
		return parseClass(classes);
	}
	
	private static String[] getPackage(Element root){
		Element e = root.element("scan");
		if(e==null){
			return null;
		}
		String[] packages = e.attributeValue("package").split(",");
		return packages;
	}
	
	private static List<Class> getClasses(String[] packages){
		List<Class> list = new ArrayList<Class>();
		String classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		for(String pack : packages){
			System.out.println("path:"+classpath+pack.replace(".","/"));
			File file = new File(classpath+pack.replace(".","/"));
			File[] files = file.listFiles();
			for(File f : files){
				if(!f.isDirectory()){
					try {
						String fname = f.getName().substring(0, f.getName().lastIndexOf("."));
						System.out.println(pack+"."+fname);
						list.add(Class.forName(pack+"."+fname));
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return list;
	}
	
	private static Map<String,String> parseClass(List<Class> list){
		Map<String,String> map= new HashMap<String, String>();
		for(Class clazz : list){
			//Action（Control）类名
			Controller controller = (Controller) clazz.getAnnotation(Controller.class);
			if(controller!=null){
				//Action（Control）类的ReqMapping注解
				RequestMapping req_class = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
				Method[] methods = clazz.getMethods();
				for(Method m : methods){
					//方法的ReqMapping注解
					RequestMapping req = m.getAnnotation(RequestMapping.class);
					if(req!=null){
						StringBuffer paraNames = new StringBuffer();
						for(Class para : m.getParameterTypes()){
							//方法的参数
							paraNames.append(","+para.getName());
						}
						String url = req.value();
						//装载map   key=类url+方法url+webxml的urlPattern
						//value = 类名||方法名||参数
						map.put(req_class.value()+url+getUrlPattern(null), clazz.getName()+"||"+m.getName()+"||"+paraNames.substring(1));
					}
				}
			}
			
		}
		return map;
	}
	
	private static String getUrlPattern(String servletName){
		return ".do";
	}
	
	public static void main(String args[]){
		Map<String,String> data = LoadSpringMVCConfigUtil.LoadSpringMVCConfig("F:\\WORKSPACE_ECLIPSE\\xbj-spring-mvc\\WebContent\\WEB-INF\\springmvc.xml");
		for(String key:data.keySet()){
			String value = data.get(key);
			System.out.println("key:"+key+"@@@value:"+value);
		}
	}
}
